package coreplugin

import (
	"context"

	"gitee.com/go-caipu-team/go-caipu-plugin-sdk/backend"

	"go-caipu/pkg/plugins/backendplugin"
	"go-caipu/pkg/plugins/log"
)

// corePlugin system core use plugin
type corePlugin struct {
	pluginID string
	logger   log.Logger
	backend.CheckHealthHandler
	backend.CallResourceHandler
	backend.QueryDataHandler
	backend.StreamHandler
}

func New(ops backend.ServeOpts) backendplugin.PluginFactoryFunc {
	return func(pluginID string, logger log.Logger, end []string) (backendplugin.Plugin, error) {
		return &corePlugin{
			pluginID:            pluginID,
			logger:              logger,
			CheckHealthHandler:  ops.CheckHealthHandler,
			CallResourceHandler: ops.CallResourceHandler,
			QueryDataHandler:    ops.QueryDataHandler,
			StreamHandler:       ops.StreamHandler,
		}, nil
	}
}

func (c *corePlugin) PluginID() string {
	return c.pluginID
}

func (c *corePlugin) Logger() log.Logger {
	return c.logger
}

func (c *corePlugin) Start(ctx context.Context) error {
	return nil
}

func (c *corePlugin) Stop(ctx context.Context) error {
	return nil
}

func (c *corePlugin) IsManaged() bool {
	return true
}

func (c *corePlugin) Exited() bool {
	return false
}

func (c *corePlugin) Decommission() error {
	return nil
}

func (c *corePlugin) IsDecommissioned() bool {
	return false
}

func (c *corePlugin) Target() backendplugin.Target {

	return backendplugin.TargetInMemory
}

func (c *corePlugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
	return nil, backendplugin.ErrMethodNotImplemented
}
func (c *corePlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
	if c.CheckHealthHandler != nil {
		return c.CheckHealthHandler.CheckHealth(ctx, req)
	}

	return nil, backendplugin.ErrMethodNotImplemented
}

func (c *corePlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
	if c.QueryDataHandler != nil {
		return c.QueryDataHandler.QueryData(ctx, req)
	}

	return nil, backendplugin.ErrMethodNotImplemented
}

func (c *corePlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
	if c.CallResourceHandler != nil {
		return c.CallResourceHandler.CallResource(ctx, req, sender)
	}

	return backendplugin.ErrMethodNotImplemented
}
func (c *corePlugin) Execute(ctx context.Context, req *backend.ExecuteRequest) (*backend.ExecuteResponse, error) {
	if c.CallResourceHandler != nil {
		return c.CallResourceHandler.Execute(ctx, req)
	}
	return nil, backendplugin.ErrMethodNotImplemented
}

func (c *corePlugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
	if c.StreamHandler != nil {
		return c.StreamHandler.SubscribeStream(ctx, req)
	}
	return nil, backendplugin.ErrMethodNotImplemented
}

func (c *corePlugin) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
	if c.StreamHandler != nil {
		return c.StreamHandler.PublishStream(ctx, req)
	}
	return nil, backendplugin.ErrMethodNotImplemented
}

func (c *corePlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
	if c.StreamHandler != nil {
		return c.StreamHandler.RunStream(ctx, req, sender)
	}
	return backendplugin.ErrMethodNotImplemented
}
